/*
* ============================================================================
*  Name     : GV60SettingsSettingList.cpp
*  Part of  : GV60
*  Copyright: Wipo Software
* ============================================================================
*/

#include <uikon.hrh>
#include <coemain.h>
#include <aknnotewrappers.h> 
#include <aknsettingitemlist.h>
#include <coeaui.h>
#include <barsread.h>

#include <GV60.rsg>
#include "GV60.hrh"
#include "GV60SettingsSettingList.h"
#include "Logger.h"


CGV60SettingsSettingList* CGV60SettingsSettingList::NewL(CGV60Configuration& n_iConfiguration)
{
  CGV60SettingsSettingList* self = CGV60SettingsSettingList::NewLC(n_iConfiguration);
  CleanupStack::Pop(self);
  return self;
}

CGV60SettingsSettingList* CGV60SettingsSettingList::NewLC(CGV60Configuration& n_iConfiguration)
{
  CGV60SettingsSettingList* self = new (ELeave) CGV60SettingsSettingList(n_iConfiguration);
  CleanupStack::PushL(self);
  return self;
}

CGV60SettingsSettingList::CGV60SettingsSettingList(CGV60Configuration& n_iConfiguration) :
  CAknSettingItemList()
{
  m_iUsername.Copy(n_iConfiguration.GetUsername());
  m_iPassword.Copy(n_iConfiguration.GetPassword());
  
  m_iInitialSync = EGV60InitialSycnNo;  
  if (n_iConfiguration.GetInitialSync())
  {
    m_iInitialSync = EGV60InitialSycnYes;
  }
}

CGV60SettingsSettingList::~CGV60SettingsSettingList()
{
  // no specific destruction code required - no owned data
}


void CGV60SettingsSettingList::SizeChanged()
{
  CCoeControl::SizeChanged();

  if (ListBox()) 
  {
    ListBox()->SetRect(Rect());
  }
}

void CGV60SettingsSettingList::HandleResourceChange(TInt aType)
{
  CCoeControl::HandleResourceChange(aType);

  if (aType == KEikDynamicLayoutVariantSwitch)
  {
    TRect iRect;
    AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, iRect);
    SetRect(iRect);
  }
}

void CGV60SettingsSettingList::EditItemL(TInt aIndex, TBool aCalledFromMenu)
{
  CAknSettingItemList::EditItemL(aIndex, aCalledFromMenu);
  
  // Used to force a refresh to fix issue with password
  HandleChangeInItemArrayOrVisibilityL();
}

void CGV60SettingsSettingList::EditCurrentItemL()
{
  EditItemL(ListBox()->CurrentItemIndex(), ETrue);  // invoked from menu
}

CAknSettingItem* CGV60SettingsSettingList::CreateSettingItemL(TInt aIdentifier) 
{
  CAknSettingItem* pSettingItem = NULL;
  switch (aIdentifier)
  {
    case EGV60SettingsUsernameId:
      pSettingItem = new (ELeave) CAknTextSettingItem(aIdentifier, m_iUsername);
      break;
    case EGV60SettingsPasswordId:
      pSettingItem = new (ELeave) CAknPasswordSettingItem(aIdentifier, CAknPasswordSettingItem::EAlpha, m_iPassword);
      break;
    case EGV60SettingsInitialSyncId:
      pSettingItem = new (ELeave) CAknEnumeratedTextPopupSettingItem(aIdentifier, m_iInitialSync);
      break;
    default:
      break;
  }
  return pSettingItem;
}

void CGV60SettingsSettingList::StoreSettings()
{
  TRAP_IGNORE( CAknSettingItemList::StoreSettingsL() );
}

TKeyResponse CGV60SettingsSettingList::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
{
  // No action if not key event
  if (aType != EEventKey)
  {
    return EKeyWasNotConsumed;
  }

  return CAknSettingItemList::OfferKeyEventL(aKeyEvent, aType);
}

TBool CGV60SettingsSettingList::GetValues(CGV60Configuration& n_iConfiguration)
{
  n_iConfiguration.SetUsername(m_iUsername);
  n_iConfiguration.SetPassword(m_iPassword);
  
  if (m_iInitialSync == EGV60InitialSycnYes)
  {
    n_iConfiguration.SetInitialSync(ETrue);
  }
  else
  {
    n_iConfiguration.SetInitialSync(EFalse);
  }

  return ETrue;
}
